package com.shu.es;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class PutTemplate {
	
	//private static String url = "http://192.168.1.18:9200/_template/";
	//private static String url = "http://192.168.1.29:9200/_template/";
	//private static String url = "http://192.168.1.16:9200/_template/";
	private static String url = "http://127.0.0.1:9200/_template/";
	//private static String url = "http://127.0.0.1:9201/_template/";  //备份环境
	//private static String url = "http://127.0.0.1:9222/_template/";

	public static void main(String[] args) throws Exception {
		//File file = new File("C:\\svn_file\\技术\\05 备份\\es_template_tx");
		File file = new File("C:\\svn_file\\技术\\05 备份\\es_template");
		//File file = new File("C:\\svn_file\\技术\\05 备份\\es_template_hb_backup");
		File[] listFiles = file.listFiles();
		for(File f : listFiles) {
			String template = f.getName().split("\\.")[0];
			String contant = txt2String(f);
			if(!f.isDirectory()) {
				System.out.println("             "+f.getName()+"||||"+contant);
				httpPut2(template,contant);
			}
		}
	}
	
	public static void httpPut2(String name,String contents) {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();     
        HttpPut httpput = new HttpPut(url+name);  
          
        //设置header  
        httpput.setHeader("Content-type", "application/json");      
        //组织请求参数    
        StringEntity stringEntity = new StringEntity(contents, "utf-8");    
        httpput.setEntity(stringEntity);    
        String content = null;    
        CloseableHttpResponse  httpResponse = null;    
        try {    
            //响应信息  
            httpResponse = closeableHttpClient.execute(httpput);    
            HttpEntity entity = httpResponse.getEntity();    
            content = EntityUtils.toString(entity, "utf-8");  
            if(!"{\"acknowledged\":true}".equals(content)) {
            	System.out.println("ERROR=========>"+content);
            }
        } catch (Exception e) {    
            e.printStackTrace();    
        }finally{    
            try {    
                httpResponse.close();    
                closeableHttpClient.close();  //关闭连接、释放资源    
            } catch (IOException e) {    
                e.printStackTrace();    
            }    
        }    
        return ;    
	}
	
	 public static void httpPut(String fileName,String contant) {
	        String urlStr =url +fileName;

	        //establish connection and push policy to snc controller
	        try {
	             URL url = new URL(urlStr);
	             HttpURLConnection conn = (HttpURLConnection)url.openConnection();
	             conn.setRequestMethod("PUT");
	             conn.setDoInput(true);
	             conn.setDoOutput(true);
	             OutputStream os = conn.getOutputStream();     
	             os.write(contant.getBytes("utf-8")); 
	             os.flush();
	             os.close();         
	             BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	             String line = "";
	             String result = "";
	             while( (line =br.readLine()) != null ){
	                 result += line;
	             }
	             br.close();
	        } catch (Exception e) {
	            e.printStackTrace();  
	        }
	    }
	 
	 /**
	     * 读取txt文件的内容
	     * @param file 想要读取的文件对象
	     * @return 返回文件内容
	     */
	    public static String txt2String(File file){
	        StringBuilder result = new StringBuilder();
	        try{
	            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
	            String s = null;
	            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
	                result.append(s);
	            }
	            br.close();    
	        }catch(Exception e){
	            e.printStackTrace();
	        }
	        return result.toString();
	    }
}
